package com.lighu.computing.utils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lighu.computing.dao.DatabaseLaunch;
import com.lighu.computing.model.ConstantFields;

/**
 * @Author : LingHu
 * @Email : 1714873054@qq.com
 * @Date : on 2023/10/4 16:37.
 * @Description :描述
 */
public class SqlPlantUtils {
    private final DatabaseLaunch conn;//连接
    private final SQLiteDatabase db;//数据库

    public SqlPlantUtils(Context context) {
        conn=new DatabaseLaunch(context);
        db = conn.getWritableDatabase();//初始化数据库
    }

    //关闭数据库
    public void closeSql(){
        db.close();
    }

    //增删改查
    public void insert(Integer id,String name,Integer age,Integer salary){
        String sql=
                "insert into "+ ConstantFields.TABLE_NAME+"(id,name,age,salary) values(?,?,?,?)";
        db.execSQL(sql,new Object[]{id,name,age,salary});
    }
    //删除
    public void delete(Integer id){
        String sql=
                "delete from "+ConstantFields.TABLE_NAME+" where id=id";
        db.execSQL(sql);
    }

    public void query(){
        String sql=
                "select * from "+ConstantFields.TABLE_NAME;
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()){
            int index = cursor.getColumnIndex("name");
            String name = cursor.getString(index);
            System.out.println("name="+name);
        }
        cursor.close();
    }

    public void update(Integer salaries,Integer ages){
       String sql=
                "update "+ConstantFields.TABLE_NAME+" set salary="+salaries+ " where age="+ages;
        db.execSQL(sql);
    }

}
